package com.ysw.service;

import com.google.common.collect.Lists;
import com.ysw.domian.Details;
import com.ysw.domian.PushTaskLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName MongoApiService
 * @Description api
 * @Author ysw
 * @Date 2021/11/4 12:32
 */
@Service
public class MongoApiService {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 插入多条
     */
    public void testInsertMany() {
        ArrayList<Object> logs = Lists.newArrayList();
        for (int i = 0; i < 10; i++) {
            List<Details> details = Lists.newArrayList(new Details(i + "token", "timestamp" + i, "uuid" + i));
            PushTaskLog pushTaskLog = new PushTaskLog(null, Long.valueOf(i), "徐凤年", "servercode1", details);
            logs.add(pushTaskLog);
        }
        mongoTemplate.insert(logs,PushTaskLog.class);
    }

    /**
     * 插入单条
     */
    public void testInsert(){
        mongoTemplate.insert( new PushTaskLog(null, 111L, "徐凤年", "servercode1", Lists.newArrayList()));
    }

    /**
     * 测试更新
     * 排序
     * query.with(Sort.by(Sort.Order.desc("age")));
     *  Query query = Query.query(
     *          Criteria.where(CONSTS.DEVICE_SERIAL_FIELD).is(deviceSerial));
     *         // 每页五个
     *         Pageable pageable = new PageRequest(pageIndex, pageSize); // get 5 profiles on a page
     *         query.with(pageable);
     */
    public void testFind(){
        Query query = new Query();
       // PageRequest.of(1,5);
        query.addCriteria(Criteria.where("task_id").gt(1)).with(Sort.by(Sort.Order.desc("task_id"))).with(PageRequest.of(0, 5));
        List<PushTaskLog> pushTaskLogs = mongoTemplate.find(query, PushTaskLog.class);
        System.out.println(pushTaskLogs);
    }

    public void aggFind(){

    }


}